using UnityEngine;
using System.Collections;

public class MyGUIText : MonoBehaviour {

    public string Text;
    int LetterAmount;

    public int FramesCountX;
    public int FramesCountY;
    public float Spacing;
	
	public Material[] GUIMaterials;
	
    float frameHeight;
    float frameWidth;

	public void SetText(string newText){
		Text = newText;
		LetterAmount = Text.Length;
		
		for (int i = 0; i < Text.Length; i++)
        {
            SetLetter(Text[i], i);
        }
	}
	
    public int GetFrameNumber(char letter)
    {
        return ((int)letter );

    }
    public void SetFrame(int frameCount, int position)
    {
        if ((frameCount < 0) || (frameCount >= FramesCountX * FramesCountY))
        {
            Debug.Log("Letter not supported");
            return;
        }

        int row = frameCount / FramesCountX;
        int col = frameCount % FramesCountX;
		
		UV[position * 4].x = (frameWidth *col);
        UV[position * 4].y = 1.0f-(frameHeight * row);

        UV[position * 4+2].x = UV[position * 4].x;
        UV[position * 4+2].y = UV[position * 4].y - frameHeight;

        UV[position * 4 + 3].x = UV[position * 4].x + frameWidth;
        UV[position * 4 + 3].y = UV[position * 4].y - frameHeight;
		
        UV[position * 4 + 1].x = UV[position * 4].x + frameWidth;
        UV[position * 4 + 1].y = UV[position * 4].y;
		
        mesh.uv = UV;
    }
	
    public void SetLetter(char letter, int position)
    {
        SetFrame(GetFrameNumber(letter),position);
    }

    public Vector2 Size = new Vector2(100, 100);

    public Vector2[] UV;

    Vector3[] vertices;

    Mesh mesh;
    void Awake()
    {
        if (this.GetComponent<MeshFilter>() != null)
        {
			//mesh = this.GetComponent<MeshFilter>().sharedMesh;
            return;
        }
		LetterAmount = Text.Length;
//		Debug.Log(Text.Length);
		
        MeshFilter filter = this.gameObject.AddComponent<MeshFilter>();
        vertices = new Vector3[LetterAmount * 4];
        UV = new Vector2[LetterAmount * 4];

        for (int i = 0; i < LetterAmount; i++)
        {
            vertices[i * 4 + 0] = new Vector3(-Size.x / 2.0f + (Spacing +Size.x)*i, Size.y / 2.0f, 0);
            vertices[i * 4 + 1] = new Vector3(Size.x / 2.0f + (Spacing + Size.x) * i, Size.y / 2.0f, 0);
            vertices[i * 4 + 2] = new Vector3(-Size.x / 2.0f + (Spacing + Size.x) * i, -Size.y / 2.0f, 0);
            vertices[i * 4 + 3] = new Vector3(Size.x / 2.0f + (Spacing + Size.x) * i, -Size.y / 2.0f, 0);
        }
        mesh = new Mesh();
        mesh.vertices = vertices;
		
        frameHeight = 1.0f / (float)FramesCountY;
        frameWidth = 1.0f / (float)FramesCountX;
				
        for (int i = 0; i < LetterAmount; i++)
        {
            SetLetter(Text[i], i);
        }

        int[] indices = new int[6 * LetterAmount];
        for (int i = 0; i < LetterAmount; i++)
        {
            indices[i*6+0] = 0 + i*4;
            indices[i*6+1] = 1+ i*4;
            indices[i*6+2] = 2+ i*4;
            indices[i*6+3] = 2+ i*4;
            indices[i*6+4] = 1+ i*4;
            indices[i * 6 + 5] = 3+ i*4;
        }

        mesh.SetIndices(indices, MeshTopology.Triangles, 0);

        filter.sharedMesh = mesh;

        MeshRenderer renderer = this.gameObject.AddComponent<MeshRenderer>();

        renderer.sharedMaterial = GUIMaterials[0];
	}
	public void ChangeMaterial(int index){
		if (index < GUIMaterials.Length && index >= 0){
			this.gameObject.GetComponent<MeshRenderer>().sharedMaterial = GUIMaterials[index];
		}
	}
}
